capture program drop estimate_bydecile
program define estimate_bydecile
clear
	use `1', clear 
	collapse (mean) size `2' (median) med_size=size med_`2'=`2', by($product iso2 decile hs hs6_2002) 
	g l`2'=log(`2')
	g fdecile=log(log(11 - decile) - log(10 - decile))
	replace fdecile=log(log(1.1)) if decile==1 // this is for size from pctile 0.01 to pctile 0.1 
	replace fdecile=log(log(10)) if decile==10 // this is for size from pctile 0.9 to pctile 0.99 
	replace l`2'=log(`2' /0.9) if decile==1 |decile==10 // to account for the fact that we drop the top1% and bottom 1%
	egen c=group(iso2) 
	egen p=group($product)
	*reghdfe l`2' fdecile , a(rs=p) // no country FE id_conc level
	*fese l`2' fdecile , a(p) s(rs_) oonly // same as above but fese to get standard errors
	fese l`2' fdecile , a(hs) s(rs_hs6_) oonly // hs6 rather than id_conc with standard errors (for bootstrap) 
	reghdfe l`2'  , a(rs_nosize=hs)  // no control for transaction size (check asked at the WB) 
	reghdfe l`2' fdecile , a(rs_cty=hs c) // with country FE  
	g test=l`2' - fdecile
	reghdfe test , a(rs_alpha1=hs)  // impose alpha=1 
	collapse (mean) rs* `2', by($product hs6)
	save $rspath/rs`3'_`4', replace // Relationship stickiness (rs) measures by product are in this dataset 
	end

